package com.winapi.project.service.impl.inner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.winapi.project.common.ErrorCode;
import com.winapi.project.exception.BusinessException;
import com.winapi.project.mapper.UserInterfaceInfoMapper;
import com.winapi.project.service.UserInterfaceInfoService;
import com.winapi.winapicommon.model.entity.UserInterfaceInfo;
import com.winapi.winapicommon.service.InnerUserInterfaceInfoService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/**
 * @author yyzhang
 * @version 1.0
 */
@DubboService
public class InnerUserInterfaceInfoServiceImpl implements InnerUserInterfaceInfoService {
@Resource
private UserInterfaceInfoService userInterfaceInfoService;
@Resource
private UserInterfaceInfoMapper userInterfaceInfoMapper;
    @Override
    public boolean invokeCount(long interfaceInfoId, long userId) {
        return userInterfaceInfoService.invokeCount(interfaceInfoId,userId);
    }

    @Override
    public boolean enableInvoke(long interfaceInfoId, long userId) {
        QueryWrapper<UserInterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("interfaceInfoId",interfaceInfoId);
        queryWrapper.eq("userId",userId);
        UserInterfaceInfo userInterfaceInfo = userInterfaceInfoMapper.selectOne(queryWrapper);
        Integer leftNum = userInterfaceInfo.getLeftNum();
        if(leftNum<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"无请求次数");
        }
        return true;
    }
}
